package com.dzf.controller;


import com.dzf.entity.User;
import com.dzf.service.LoginService;
import com.dzf.service.UserSerive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Lenovo
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    UserSerive userSerive;
    @Autowired
    LoginService loginService;

    /**
     * 登录
     * @return 进入登录页面
     */
    @RequestMapping("/go2LogIn")
    public String go2LogIn(){
        return "/go2login";
    }

    /**
     * 登出
     * @param req
     * @param res
     * @throws IOException
     * @throws ServletException
     * @return
     */
    @GetMapping("/logOut")
    public void logOut(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
        res.sendRedirect("/login/index");
    }

    /**
     * 注册用户
     * @param user 用户信息
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public boolean addUser(@RequestBody User user){
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        user.setName(user.getUsername()+"桑");
        System.out.println(user);
        boolean b = userSerive.add(user);
        return b;
    }

    /**
     * 首页
     * @return
     */
    @RequestMapping("/index")
    public String homePage(){
        return "index";
    }

}


